package com.devilvan.betrice.recruitment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.devilvan.betrice.annotation.BetriceRunTime;
import com.devilvan.betrice.recruitment.mapper.BossJobMapper;
import com.devilvan.betrice.recruitment.po.BossJobPo;
import com.devilvan.betrice.recruitment.po.BossLabelPo;
import com.devilvan.betrice.recruitment.po.BossSkillPo;
import com.devilvan.betrice.recruitment.po.BossWelfarePo;
import com.devilvan.betrice.recruitment.repository.BossJobRepository;
import com.devilvan.betrice.recruitment.service.BossJobService;
import com.devilvan.betrice.recruitment.service.BossLabelService;
import com.devilvan.betrice.recruitment.service.BossSkillService;
import com.devilvan.betrice.recruitment.service.BossWelfareService;
import com.devilvan.betrice.recruitment.vo.BossJobVo;
import com.devilvan.betrice.util.UCopy;
import lombok.NonNull;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Evad.Wu
 * @Description Boss直聘-招聘信息 业务逻辑实现类
 * @date 2022-08-08
 */
@Service
public class BossJobServiceImpl extends ServiceImpl<BossJobMapper, BossJobPo> implements BossJobService {
    /**
     * 最后修改时间超过阈值，则冗余新增
     */
    private static final int PASS_DAY_THREHOLD = 15;

    @Resource(name = "bossLabelServiceImpl")
    private BossLabelService bossLabelService;
    @Resource(name = "bossSkillServiceImpl")
    private BossSkillService bossSkillService;
    @Resource(name = "bossWelfareServiceImpl")
    private BossWelfareService bossWelfareService;
    @Resource(name = "bossJobRepository")
    private BossJobRepository bossJobRepository;

    @Override
    @BetriceRunTime
    @Async(value = "threadPoolExecutor")
    @Transactional(rollbackFor = {SQLException.class, ArithmeticException.class})
    public void doBoss(@NonNull List<BossJobVo> bossJobVoList) {
        List<BossJobPo> bossJobPoList = new ArrayList<>();
        for (BossJobVo bossJobVo : bossJobVoList) {
            BossJobPo bossJobPo = new BossJobPo();
            UCopy.fullCopy(bossJobVo, bossJobPo, "skills", "welfares", "labels");
            Set<String> skills = bossJobVo.getSkills().stream().map(m -> m.trim().toUpperCase(Locale.ROOT)).collect(Collectors.toSet());
            Set<String> welfares = bossJobVo.getWelfares().stream().map(m -> m.trim().toUpperCase(Locale.ROOT)).collect(Collectors.toSet());
            Set<String> labels = bossJobVo.getLabels().stream().map(m -> m.trim().toUpperCase(Locale.ROOT)).collect(Collectors.toSet());
            Set<BossSkillPo> bossSkillPoSet = bossSkillService.checkSaveSkills(skills);
            Set<BossWelfarePo> bossWelfarePoSet = bossWelfareService.checkSaveWelfares(welfares);
            Set<BossLabelPo> bossLabelPoSet = bossLabelService.checkSaveLabels(labels);
            bossJobPo.getSkills().addAll(bossSkillPoSet);
            bossJobPo.getWelfares().addAll(bossWelfarePoSet);
            bossJobPo.getLabels().addAll(bossLabelPoSet);
            bossJobPoList.add(bossJobPo);
        }
        List<String> jobIdList = bossJobPoList.stream().map(BossJobPo::getJobId).toList();
        List<BossJobPo> isExsistJobPoList = bossJobRepository.findByJobIdIn(jobIdList);
        List<BossJobPo> ready2SaveJobPo = UCopy.checkAssemble(bossJobPoList, isExsistJobPoList, BossJobPo::getJobId, PASS_DAY_THREHOLD);
        bossJobRepository.saveAll(ready2SaveJobPo);
    }
}
